package com.pioneer.controller.stock;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.constant.StockConstants;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.stock.domain.StoInout;
import com.pioneer.web.stock.service.IStoInoutService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 采购入库单
 *
 * @author hlm
 */
@RestController
@RequestMapping("/stock/in")
public class StoInController extends BaseController {

    @Resource
    private IStoInoutService inoutService;

    /**
     * 获取采购入库单列表
     *
     * @param inout 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:list')")
    @GetMapping("/list")
    public AjaxResult list(StoInout inout) {
        boolean isPage = startPage();
        inout.setType(StockConstants.INOUT_TYPE_IN);
        List<StoInout> list = inoutService.list(Wrappers.query(inout));
        return success(isPage, list);
    }

    /**
     * 根据采购入库单id获取详细信息
     *
     * @param inoutId 采购入库单id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:query')")
    @GetMapping(value = "/{inoutId}")
    public AjaxResult getInfo(@PathVariable Long inoutId) {
        return AjaxResult.success(inoutService.selectInById(inoutId));
    }

    /**
     * 新增采购入库单
     *
     * @param inout 采购入库单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:add')")
    @Log(title = "采购入库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StoInout inout) {
        return toAjax(inoutService.addIn(inout));
    }

    /**
     * 修改采购入库单
     *
     * @param inout 采购入库单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:edit')")
    @Log(title = "采购入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StoInout inout) {
        return toAjax(inoutService.updateIn(inout));
    }

    /**
     * 删除采购入库单
     *
     * @param inoutIds 采购入库单id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:remove')")
    @Log(title = "采购入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inoutIds}")
    public AjaxResult remove(@PathVariable Long[] inoutIds) {
        return toAjax(inoutService.deleteIn(Arrays.asList(inoutIds)));
    }

    /**
     * 根据采购订单ID返回采购入库单信息
     * @param orderId 采购订单ID
     * @return 结果
     */
    @GetMapping(value = "/getInoutByOrder/{orderId}")
    public AjaxResult getInoutByOrder(@PathVariable Long orderId) {
        return AjaxResult.success(inoutService.getInoutByOrder(orderId));
    }

    /**
     * 确认入库
     * @param inout 采购入库单
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:in:in')")
    @Log(title = "采购入库单", businessType = BusinessType.OTHER)
    @PostMapping("/in")
    public AjaxResult inoutIn(@RequestBody StoInout inout) {
        return toAjax(inoutService.inoutIn(inout));
    }
}
